Ext.onReady(function() {
			Ext.Loader.setPath("org.jks", "/view/common");
			var comboStore = Ext.create('Ext.data.Store', {
						fields : ['Id', 'Name'],
						autoLoad : true,
						proxy : {
							type : 'ajax',
							url : '/device_type/all',
							reader : {
								type : 'json',
								root : 'data.rows'
							}
						}
					});
			var deviceTypeCombo = Ext.create('Ext.form.ComboBox', {
						fieldLabel : '设备类型',
						labelWidth: 100,
						editable : false,
						store : comboStore,
						displayField : 'Name',
						valueField : 'Id'
					});
			var deviceStore = Ext.create('Ext.data.Store', {
						fields : ['Id', 'No', {
									name : "Type",
									type : 'object'
								}, "Brand", "Manufacturers", "Model", {
									name : "OperationTime",
									type : 'date'
								}, {
									name : "CurrentStatus",
									type : "object"
								}],

						autoLoad : true,
						proxy : {
							type : 'ajax',
							url : '/device/listByType',
							extraParams:{
								id: -1
							},
							reader : {
								type : 'json',
								root : 'data.rows'
							}
						}
					});
			var userStore = Ext.create('Ext.data.Store', {
				fields : [{name:"Id",type:'int'}, "Username", 'Name'],
				proxy : {
					type : 'ajax',
					url : '/user/all',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				},
				autoLoad : true
			});
			
			var recipientor = Ext.create("Ext.form.ComboBox",{
				fieldLabel : '领用人',
				labelWidth : 100,
				editable : false,
				allowBlank: false,
				id : 'User',
				store : userStore,
				queryMode : 'local',
				valueField : 'Id',
				displayField : 'Name'
			});
			var workStore = Ext.create('Ext.data.Store', {
				fields : [{name:"Id",type:'int'}, 'Name'],
				proxy : {
					type : 'ajax',
					url : '/work/list_no_finish',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				},
				autoLoad : true
			});
			setTimeout(function(){
				workStore.insert(0, [{Id: 0, Name:'无'}]);
			}, 1000);
			
			
			var workCombo = Ext.create('Ext.form.ComboBox', {
				fieldLabel : '选择作业',
				labelWidth: 100,
				editable : false,
				store : workStore,
				queryMode : 'local',
				displayField : 'Name',
				valueField : 'Id'
			});
			
			var outWindow = Ext.create('Ext.window.Window', {
						title : '出库',
						closeAction : 'hide',
						border : false,
						bodyBorder: false,
						height : 350,
						width : 400,
						layout : 'fit',
						dockedItems : [{
									xtype : 'toolbar',
									border: false,
									bodyBorder: false,
									items : [deviceTypeCombo]
								},{
									xtype: 'toolbar',
									border: false,
									bodyBorder: false,
									items:[recipientor]
								},{
									xtype: 'toolbar',
									border: false,
									bodyBorder: false,
									items:[workCombo]
								}],
						buttons : [{
									text : '确定',
									listeners : {
										click : function() {
											if(!recipientor.isValid()){
												return;
											}
											if(deviceTypeCombo.getValue() == 1 && workCombo.getValue() == 0){
												Ext.Msg.alert('错误', '选择无人机必须选择作业');
												return;
											}
											var grid = Ext.getCmp("out_device_grid");
											var selectionModel = grid.getSelectionModel();
											var models = selectionModel.getSelection();
											var ids = [];
											for(var i=0; i<models.length;i++){
												ids[i] = models[i].get("Id");
											}
											if(Ext.isEmpty(ids)){
												Ext.Msg.alert('错误', '必须选择设备');
												return;
											}
											var params = {
												ids: ids,
												RecipientId: recipientor.getValue()
											};
											if(workCombo.getValue() != null){
												params.WorkId = workCombo.getValue();
											}
											
											Ext.Ajax.request({
												url: '/device/ck',
												type: 'POST',
												params: params,
												callback:function(options,success, response){
													if(success){
														var result  = Ext.decode(response.responseText);
														if(result.success){
															deviceTypeCombo.setValue('');
															recipientor.setValue('');
															workCombo.setValue('');
															outWindow.hide();
															Ext.getCmp("device_grid").getStore().reload();
														}
													}
												}
											});
										}
									}
								}, {
									text : '取消',
									listeners : {
										click : function() {
											outWindow.hide();
										}
									}
								}],
						items : {
							xtype : 'grid',
							margin: '5 5',
							bodyBorder: false,
							id: "out_device_grid",
							selType : 'checkboxmodel',
							store : deviceStore,
							border : false,
							columns : [{text : '序号',
										xtype : 'rownumberer',
										width : 40,
										sortable : false
									}, {
										text : '设备编号',
										flex : 1,
										dataIndex : 'No'
									}, {
										text : '设备类型',
										flex : 1,
										dataIndex : 'Type',
										renderer : function(v) {
											return v.Name;
										}
									}, {
										text : '设备状态',
										flex : 1,
										dataIndex : 'CurrentStatus',
										renderer : function(v) {
											if (v.Id == 1) {
												return '<span style="color:green">'
														+ v.Name + '</span>';
											} else if (v.Id == 2) {
												return '<span style="color:#FFAA00">'
														+ v.Name + '</span>';
											} else {
												return '<span style="color:red">'
														+ v.Name + '</span>';
											}
										}
									}]
						},
						listeners : {
							afterrender : function(self, eOpts) {
								deviceTypeCombo.addListener("change", function(
												self, newValue, oldValue) {
											var store = Ext.getCmp("out_device_grid").getStore();
											store.getProxy().extraParams = {
												id : newValue
											};
											store.reload();
										});
							},
							beforeshow: function(self, eOpts){
								var store = Ext.getCmp("out_device_grid").getStore();
								store.getProxy().extraParams = {
									id : -1
								};
								store.reload();
							}
						}
					});

			var toolbar = Ext.create('Ext.toolbar.Toolbar', {
						dock : 'top',
						items : [{
							text : '出库',
							xtype : 'button',
							listeners : {
								click : function() {
									outWindow.show();
								}
							}
						},{
							text : '入库',
							xtype : 'button',
							listeners : {
								click : function() {
									Ext.create('org.jks.AddInDevice',{
										url: '/device_in_out/in_device',
										successFunc:function(){
											Ext.getCmp("device_grid").getStore().reload();
										}
									}).show();
								}
							}
						},{
							xtype: 'datefield',
							id: 'start_time',
							labelWidth: 60,
							fieldLabel: '起始时间',
							format: 'Y-m-d'
						},{
							xtype: 'datefield',
							id: 'end_time',
							labelWidth: 60,
							fieldLabel: '结束时间',
							format: 'Y-m-d'
						}, {
							xtype: 'button',
							text: '搜索',
							listeners: {
								click: function(){
									var params = {
										startTime: Ext.getCmp("start_time").getValue(),
										endTime: Ext.getCmp("end_time").getValue()
									};
									var store = Ext.getCmp("device_grid").getStore();
									store.proxy.extraParams = params;
									store.reload();
								}
							}
						},{
							xtype: 'button',
							text: '删除',
							listeners: {
								click: function(){
									var grid = Ext.getCmp("device_grid");
									var selectionModel = grid.getSelectionModel();
									var models = selectionModel.getSelection();
									var ids = [];
									for (var i = 0; i < models.length; i++) {
										ids[i] = models[i].get("Id");
									}
									Ext.Ajax.request({
												url : '/device_in_out/delete',
												type : 'POST',
												params : {
													ids : ids
												},
												callback : function(options, success,
														response) {
													if (success) {
														var result = Ext
																.decode(response.responseText);
														if (result.success) {
															Ext.getCmp("device_grid")
																	.getStore().reload();
														} else {
															Ext.Msg.alert('错误', result.msg);
														}
													}
												}
											});
								}
							}
						}, {
							xtype: 'button',
							text: '刷新',
							listeners: {
								click: function(){
									Ext.getCmp("device_grid").getStore().reload();
								}
							}
						},{
							text : '导出',
							xtype : 'button',
							listeners : {
								click : function() {
									window.open('/device_in_out/export?time='+new Date().getTime());
								}
							}
						}]
					});
			var store = Ext.create('Ext.data.Store', {
						fields : ['Id', "Type", 'TdCode', 'No', 'DeviceType', 
						'Brand', 'Manufacturers', 'Model', {
									name : "Operator",
									type : "object"
								}, {
									name : "Time"/*,
									type : "date"*/
								}],
						autoLoad : true,
						pageSize: 25,
						proxy : {
							type : 'ajax',
							url : '/device_in_out/search',
							reader : {
								type : 'json',
								root : 'data.rows',
								totalProperty: 'data.total'
							}
						}
					});
			
			var grid = Ext.create('Ext.grid.Panel', {
						id : "device_grid",
						selType : 'checkboxmodel',
						margin : '5 5',
						store : store,
						columns : [{text : '序号',
									xtype : 'rownumberer',
									width : 40,
									sortable : false
								}, {
									text : '类型',
									dataIndex : 'Type',
									flex:1,
									renderer : function(v) {
										return v == 1 ? "入库" : "出库";
									}
								}, {
									text : "设备编号",
									flex:1,
									dataIndex : 'No'
								}, {
									text : '设备类型',
									flex:1,
									dataIndex : 'DeviceType'
								}, {
									text : '领用人或入库人',
									flex:1,
									dataIndex : 'Operator',
									renderer : function(v) {
										return v.Username + "(" + v.Name + ")";
									}
								}, {
									text : '操作时间',
									dataIndex : 'Time',
									flex : 1,
									renderer : function(v) {
										return v;
									}
								}],
								bbar : Ext.create('Ext.PagingToolbar', {
									store : store,
									border: false,
									displayInfo : true,
									displayMsg : '显示{0} - {1} / 总数 {2}',
									emptyMsg : "没数据"
								})
					});
			var panel = Ext.create("Ext.panel.Panel", {
						title : '',
						border : false,
						bodyBorder : false,
						layout : 'fit',
						dockedItems : [toolbar],
						items : [grid]
					});
			Ext.create('Ext.Viewport', {
						layout : 'fit',
						style : 'background:none;background-color:white;',
						items : [panel]
					});
		});
